/*
;* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare');

(function () {
	var DQ = Ext.DomQuery;
	
	var Menu = Ext.extend(Ext.menu.Menu, {
		cls: 'compare-changeBtn-menu',
		
		modelWalk: undefined,
		commonCompetitors: undefined,
		
		anchorHeight: 10,
		
		defaultOffsets: [-50, 0],
		
		shadow: 'sides',
	
		constructor: function (cfg)
		{	
			var mw = cfg.modelWalk;
			var cc = cfg.commonCompetitors;
			cc.hideMode = 'display';
			
			cfg = Ext.apply(cfg||{}, {
				items: [mw, cc]	
			});
			
			Menu.superclass.constructor.call(this, cfg);
		},
		
		initComponent: function ()
		{
			Menu.superclass.initComponent.apply(this, arguments);
			
			this.mon(this, {
				scope: this,
				'beforeshow': this.onBeforeShow
			});
			
			this.mon(this.commonCompetitors.getStore(), {
				scope: this,
				'load': this.showCommonCompetitors,
				'datachanged': this.showCommonCompetitors,
				'clear': this.showCommonCompetitors
			});
		},
		
		/**
		 * @method onBeforeShow
		 * @private
		 */
		onBeforeShow: function ()
		{
			var cc = this.commonCompetitors;
			cc.getStore().load();
			
			//reset the model walk
			this.modelWalk.loader.doLoad();
		},
		
		showCommonCompetitors: function ()
		{
			var cc = this.commonCompetitors;
			var store = cc.getStore();
			cc[store.getCount() ? 'show' : 'hide']();
		},
		
		onRender: function ()
		{
			var dh = {
				tag: 'div',
				cls: 'anchor-wrap',
				children: {
					tag: 'b'
				}
			};
			
			Menu.superclass.onRender.apply(this, arguments);
			
			//override shadow adjustments to account for the anchor point
			var adjusts = this.el.shadow.adjusts;
			adjusts.t = adjusts.t + this.anchorHeight;
			adjusts.h = adjusts.h - this.anchorHeight;
			
			var a = this.el.child('a');
			this.el.createChild(dh, a);
		}
	});
	
	
	/**
	 * A customized Ext.Button to display a drop down menu with a model walk and common competitors listing
	 * @class ChangeButton
	 * @extends Ext.Button
	 * @namespace net.autodata.compare
	 */
	var Button = Ext.extend(Ext.Button, {
		
		ctCls: 'a-btn-change',
		
		text: 'Change',
		width: 100,
		
		constructor: function (cfg)
		{
			
			this.addEvents({
				/**
				 * @event trimselect
				 * @param {String} acode
				 * Fired when a vehicle is selected from either the Model Walk or Common Competitor list 
				 */
				'trimselect': true
			});
			
			var loader = new net.autodata.modelwalk.plugins.ModelYearLoader();
			var mw = new net.autodata.modelwalk.ModelWalkPanel({
				view: new net.autodata.modelwalk.ModelWalkMenuView(),
				plugins: loader,
				loader: loader
			});
			
			var cc = new net.autodata.compare.CommonCompetitorsPanel();
			
			
			cfg = Ext.apply(cfg ||{}, {
			    text: ASC.message('net.autodata.label.change'),
				menu: new Menu({modelWalk: mw, commonCompetitors: cc}),
				
				modelWalk: mw,
				commonCompetitors: cc
			});
			
			Button.superclass.constructor.call(this, cfg);
		},
		
		initComponent: function ()
		{
			Button.superclass.initComponent.apply(this, arguments);
			
			this.modelWalk.trim.on('select', this.onTrimSelect, this);
			this.commonCompetitors.on('select', this.onTrimSelect, this);
		},
		
		
		/**
		 * @method onTrimSelect
		 * @private
		 */
		onTrimSelect: function (target, record)
		{	
			var acode = record ? record.id : undefined;
			
			if (!Ext.isEmpty(acode) && this.menu && !this.menu.hidden) {
				this.menu.hide(true);
				this.fireEvent('trimselect', acode);
			}
		}
		
	});
	
	net.autodata.compare.ChangeButton = Button;
	
	Ext.reg('a-btn-change', net.autodata.compare.ChangeButton);
	
})();
